﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Ecopsy.Tools
{
    /// <summary>
    /// Object Data Source.
    /// в качестве источника - делегат на функцию 
    /// Формат делегата -  delegate object[]  GetObjectSet (object where)
    /// </summary>
    public class ObjectDS_F : IEnumerator, IEnumerable
    {        
        public delegate object[]  GetObjectSet (object where);
        
        private int _сurrent = 0;
        private object[] _arr = null;
        
        private GetObjectSet _SourceFunction = null;
        private object _ParamOfFunction = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="SourceFunction"></param>
        /// <param name="ParamOfFunction"></param>
        public  ObjectDS_F(GetObjectSet SourceFunction, object ParamOfFunction)
        {
            _SourceFunction = SourceFunction;
            _ParamOfFunction = ParamOfFunction;       
        }

        public ObjectDS_F() { }

        private  void InitSource(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance= this;
        }
        
      
      
        #region IEnumerator Members

        public object Current
        {
            get { return _arr[_сurrent]; }
        }

        public bool MoveNext()
        {
            if (_сurrent + 1 > _arr.Length) return false;   
            _сurrent++;
            return true;


        }

        public void Reset()
        {
            _сurrent = 0;
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return this;
        }

        #endregion

        #region Select
        public object[] Select()
        {
            _arr = _SourceFunction(_ParamOfFunction);
            return _arr;
        }
        
        public object[] Select(int startRowIndex, int maximumRows)
        {
            _arr = _SourceFunction(_ParamOfFunction);
            return _arr;
        }

        public int GetCount()
        {            
            return _arr.Length;
        }

        #endregion
        
        

        public  ObjectDataSource ImAsObjectDataSource
        {
            get 
            {
                ObjectDataSource Result;
                Result = new ObjectDataSource(this.GetType().FullName, "Select");                
                Result.SelectCountMethod = "GetCount";
                Result.ObjectCreating +=new ObjectDataSourceObjectEventHandler(InitSource);
                return Result;
            }
        }

        
    }
}
